﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Net_SCADA
{
    public class ProcessBatch
    {
        public Dictionary<int , ProcessLoad> DictProcessLoad { get; set; } = new Dictionary<int , ProcessLoad>();//处理中的料框信息
        public DateTime CreateTime { get; set; }//批次创建时间
        public DateTime StopTime { get;  set; }//结束批次的时
        public DateTime EndTime { get; set; }//批次内没有批次的时候
        public string ProductVersion { get; set; }//处理的物料
        public string ProcuctInfo0 { get; set; }//零件名称
        public List<int> KeyAlarmID { get; private set; } = new List<int>();//隔离报警地址
        public List<int> LampAlarmID { get; private set; } = new List<int>();//亮灯报警地址
        public string CreateUser { get; set; }//创建批次的用户
        public StringBuilder BatchSerial { get; set; }//批次序列号
        public bool isBatchRunning { get; set; } = false;
        public bool isBatchStop { get; set; } = false;

        public void SetProductAlarmSettting()
        {
            DataTable dt=DBOperate.GetProductInfo(ProductVersion);
            if (dt == null || dt.Rows.Count == 0) return;
            string s=dt.Rows[0]["lampalarm"].ToString();
            if (s.Trim() != "")
            {
                string[] v=s.Split(new char[]{'|' },StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0 ; i < v.Length ; i++)
                {
                    if (int.TryParse(v[i] , out int value))
                        LampAlarmID.Add(value);
                }
            }
            s=dt.Rows[0]["keyalarm"].ToString();
            if (s.Trim() != "")
            {
                string[] v=s.Split(new char[]{'|' },StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0 ; i < v.Length ; i++)
                {
                    if (int.TryParse(v[i] , out int value))
                        KeyAlarmID.Add(value);
                }
            }
        }

        public void CheckAndEndProcessLoad(List<int> AllLoadSerialInLine)
        {
            List<int> v=new List<int>();
            if (DictProcessLoad.Count == 0) return;
            foreach (int item in DictProcessLoad.Keys)
            {
                if (AllLoadSerialInLine.IndexOf(item) == -1)
                {
                    DictProcessLoad[item].EndProcessLoad();
                    v.Add(item);
                }
            }
            foreach (var item in v)
            {
                DictProcessLoad.Remove(item);
            }
        }

        public void EndProcessBatch()
        {
            EndTime = DateTime.Now;
            isBatchRunning = false;
            DBOperate.SaveBatchInfo(BatchSerial , new Dictionary<string , string>() {
                { "endtime",EndTime.ToString("yyyy-MM-dd HH:mm:ss") },
                { "isbatchrunning","0"}
            });
        }

        public bool SaveNewBatchInfo()//保存批次信息到数据库
        {
            return DBOperate.SaveBatchInfo(BatchSerial , new Dictionary<string , string>() {
                { "createtime",CreateTime.ToString("yyyy-MM-dd HH:mm:ss") },
                { "createuser",CreateUser },
                { "isbatchrunning","1"},
                { "productversion",ProductVersion }
            });
        }
        public void BatchStop()
        {
            StopTime = DateTime.Now;
            isBatchStop = true;
            DBOperate.SaveBatchInfo(BatchSerial , new Dictionary<string , string>() {
                { "stoptime",StopTime.ToString("yyyy-MM-dd HH:mm:ss") }
            });
        }
    }
}
